<div class="row">
  <button type="button" id="start-btn" class="btn btn-primary disabled">启动月K比对</button>
  <button type="button" id="stop-btn" class="btn btn-danger disabled">关闭月K比对</button>
  <button type="button" id="w-btn" class="btn btn-default disabled"></button>
  <button type="button" id="r-btn" class="btn btn-default disabled"></button>
  <p/>
  <div class="panel panel-default">
    <div id="connect-status" class="panel-body"></div>
  </div>
</div>
<div class="row">
  <table class="mitake-table" border="1">
    <thead>
      <tr>
      	<th>机房</th>
      	<th>响应</th>
      	<th>代码</th>
        <th>日期</th>
        <th>时间</th>
        <th>开盘价</th>
        <th>最高价</th>
        <th>最低价</th>
        <th>收盘价</th>
        <th>总量</th>
        <th>参考价</th>
      </tr>
    </thead>
    <tbody id="list-tbody">
    </tbody>
  </table>
</div>
<script type="text/javascript">
var updateConnectStatus = '';
var pushIndex = -1;

$(document).ready(function()
{
	clearInterval(updateConnectStatus);
	updateConnectStatus = setInterval("getConnectStatus()", 1000);
	
	$('#start-btn').on('click', function() 
	{
		$('#start-btn').addClass('disabled');
		$('#stop-btn').removeClass('disabled');
		startService();
 	});

	$('#stop-btn').on('click', function() 
	{
		$('#start-btn').removeClass('disabled');
		$('#stop-btn').addClass('disabled');
		
		stopService();
 	});
});

function isRunning()
{
	send('/monthk', 'event=t', function callbackComplete(jqXHR, textStatus)
	{}, 
	function callbackError(jqXHR, textStatus, errorThrown)
	{
		$('#message-body').text(errorThrown);
		$('#message-dialog').modal('show');
	}, 
	function callbackSuccess(data, textStatus, jqXHR)
	{
		var info = JSON.parse(data);
		
		if(info.code == '00000')
		{
			if(info.running == 'true')
			{
				$('#stop-btn').removeClass('disabled');
			}
			else
			{
				$('#start-btn').removeClass('disabled');
			}
		}
		else
		{
			$('#message-body').text(info.message);
			$('#message-dialog').modal('show');
		}
	});
}

function startService()
{
	send('/monthk', 'event=s', function callbackComplete(jqXHR, textStatus)
	{}, 
	function callbackError(jqXHR, textStatus, errorThrown)
	{
		$('#message-body').text(errorThrown);
		$('#message-dialog').modal('show');
	}, 
	function callbackSuccess(data, textStatus, jqXHR)
	{
		var info = JSON.parse(data);
		
		//updateConnectStatus = setInterval("getConnectStatus()", 1000);
		
		//$('#message-body').text(info.message);
		//$('#message-dialog').modal('show');
	});
}

function stopService()
{
	send('/monthk', 'event=e', function callbackComplete(jqXHR, textStatus)
	{}, 
	function callbackError(jqXHR, textStatus, errorThrown)
	{
		$('#message-body').text(errorThrown);
		$('#message-dialog').modal('show');
	}, 
	function callbackSuccess(data, textStatus, jqXHR)
	{
		var info = JSON.parse(data);
		
		$('#connect-status').html('');
		//$('#message-body').text(info.message);
		//$('#message-dialog').modal('show');
	});
}

function getConnectStatus()
{
	send('/monthk', 'event=g&src=' + pushIndex, function callbackComplete(jqXHR, textStatus)
	{}, 
	function callbackError(jqXHR, textStatus, errorThrown)
	{
		$('#message-body').text(errorThrown);
		$('#message-dialog').modal('show');
	}, 
	function callbackSuccess(data, textStatus, jqXHR)
	{
		var info = JSON.parse(data);

		if(info.code == '00000')
		{
			if(info.running == 'true')
			{
				$('#start-btn').removeClass('disabled');
				$('#stop-btn').removeClass('disabled');
				
				$('#start-btn').addClass('disabled');
				$('#stop-btn').removeClass('disabled');
				
				if(info.host.length > 0)
				{
					var s = '';
					
					for(i = 0 ; i < info.host.length ; i++)
					{
						s += '(' +info.host[i].ip + '_' + (info.host[1].status == '0' ? "连线中" : "<font style=\"color:#990000;\">断线中</font>") + '),';
					}

					s += '数据比对中.....';
					
					$('#w-btn').text('发送报文数量:' + info.w);
					$('#r-btn').text('回应报文数量:' + info.r);
					$('#connect-status').html(s);
				}
				
				if(info.push.length > 0)	
				{
					for(i = 0 ; i < info.push.length ; i++)
					{
						$('#list-tbody').append('<tr>');
						
// 						$('#list-tbody').append('<td>');
// 						$('#list-tbody').append('33333');
// 						$('#list-tbody').append('</td>');

						$('#list-tbody').append('<td>' + info.push[i].host + '</td>');
						$('#list-tbody').append('<td>' + info.push[i].responseTime + '</td>');
						$('#list-tbody').append('<td>' + info.push[i].name + '</td>');
						$('#list-tbody').append('<td' + (info.push[i].date.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].date.v + '</td>');
						$('#list-tbody').append('<td' + (info.push[i].time.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].time.v + '</td>');
						$('#list-tbody').append('<td' + (info.push[i].openPrice.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].openPrice.v + '</td>');
						$('#list-tbody').append('<td' + (info.push[i].highPrice.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].highPrice.v + '</td>');
						$('#list-tbody').append('<td' + (info.push[i].lowPrice.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].lowPrice.v + '</td>');
						$('#list-tbody').append('<td' + (info.push[i].preClosePrice.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].preClosePrice.v + '</td>');
						$('#list-tbody').append('<td' + (info.push[i].volume.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].volume.v + '</td>');
						$('#list-tbody').append('<td' + (info.push[i].refPrice.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].refPrice.v + '</td>');
						
						$('#list-tbody').append('</tr>');
					}
					
					pushIndex = parseInt(info.index);
				}
			}
			else
			{
				$('#connect-status').html('');
				
				$('#start-btn').removeClass('disabled');
				$('#stop-btn').removeClass('disabled');
				
				$('#start-btn').removeClass('disabled');
				$('#stop-btn').addClass('disabled');
			}
		}
		else
		{
			$('#message-body').text(info.message);
			$('#message-dialog').modal('show');
		}
	});
}
</script>